//
//  ManyDaysViewController.m
//  MonitoringNetwork
//
//  Created by admin on 2017/11/23.
//  Copyright © 2017年 Shenzhen Xinwo Transport. All rights reserved.
//

#import "ManyDaysViewController.h"
#import "MineViewController.h"
#import "ManyDayView.h"
#import "ManyDayTitleView.h"
#import "TrendChartView.h"
#import "DaysAlarmView.h"
#import "DaysConditionsView.h"


#import "MJExtension.h"

#import "OwnerModel.h"
#import "UseModel.h"
#import "CityModel.h"
#import "ProvinceModel.h"
#import "OwnerChilderModel.h"

#import "NSDateFormatter+Category.h"
#import "NSString+File.h"



@interface ManyDaysViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)ManyDayView *leftView;
@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)TrendChartView *trendView;
@property (nonatomic,strong)DaysConditionsView *daysConditionsVie;
@property (nonatomic,strong)DaysAlarmView *daysAlarmView;
@property (nonatomic,strong)UIScrollView *contentScrollView;
@property (nonatomic,strong)ManyDayTitleView *titleView;
@property (nonatomic,strong)UIView *canvasView;
@property (nonatomic,strong)NSArray *usedArr;
@property (nonatomic,strong)NSArray *ownerArr;
@property (nonatomic,strong)NSArray *areaArr;
@property (nonatomic,strong)NSDictionary *paramDic;
@property (nonatomic,assign)NSInteger nowIndex;
@property (nonatomic,assign)NSInteger currentIndex;
@end

@implementation ManyDaysViewController

- (UIView *)canvasView
{
    if (!_canvasView) {
        _canvasView  =[UIView new];
        _canvasView.frame = CGRectMake(0, 0, kScreenWidth * 3, kScreenHeight-NavationBarHeight-40);
    }
    return _canvasView;
}
- (TrendChartView *)trendView
{
    if (!_trendView) {
        _trendView = [TrendChartView new];
    }
    return _trendView;
}
- (DaysConditionsView *)daysConditionsVie
{
    if (!_daysConditionsVie) {
        _daysConditionsVie = [DaysConditionsView new];
    }
    return _daysConditionsVie;
}
- (DaysAlarmView *)daysAlarmView
{
    if (!_daysAlarmView) {
        _daysAlarmView = [DaysAlarmView new];
    }
    return _daysAlarmView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationTitleWithTitle:@"多日统计"];
    [self loadUI];
    [self loadSbuViewDataWithIndex:0];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(allowDaysRefresh) name:@"allowDaysRefresh" object:nil];
    [self loadSbuViewDataWithIndex:0];
    [_trendView loadDataBackGroundWithParam:_paramDic];
    [_daysAlarmView loadDataBackGroundWithParam:_paramDic];
    [_daysConditionsVie loadDataBackGroundWithParam:_paramDic];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}
- (void)allowDaysRefresh
{
    _trendView.needRefresh = YES;
    _daysConditionsVie.needRefresh = YES;
    _daysAlarmView.needRefresh = YES;
    _trendView.param = _paramDic;
    _daysAlarmView.param = _paramDic;
    _daysConditionsVie.param = _paramDic;
    
    [_trendView loadDataBackGroundWithParam:_paramDic];
    [_daysAlarmView loadDataBackGroundWithParam:_paramDic];
    [_daysConditionsVie loadDataBackGroundWithParam:_paramDic];
    
}
#pragma mark - UI设置
- (void)loadUI{
    
    //设置导航栏左右按钮
    UIView *leftContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    UIButton *letfButton = [[UIButton alloc] initWithFrame:leftContentView.bounds];
    [letfButton setBackgroundImage:[UIImage imageNamed:@"screeNormal"] forState:UIControlStateNormal];
    [letfButton setBackgroundImage:[UIImage imageNamed:@"screeSel"] forState:UIControlStateHighlighted];
    [letfButton addTarget:self action:@selector(leftItemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [leftContentView addSubview:letfButton];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftContentView];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UIView *rightContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    UIButton *rightButton = [[UIButton alloc] initWithFrame:rightContentView.bounds];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"personNormal"] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"personSel"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(rightItemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [rightContentView addSubview:rightButton];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightContentView];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
    Kweaself
    self.titleView = [ManyDayTitleView new];
    [self.view addSubview:self.titleView];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.view.mas_top).offset(NavationBarHeight);
    }];
    [weakSelf.titleView setTitleSelectIndex:^(NSInteger index) {
        [weakSelf SectionSelectIndex:index];
    }];
    [self.titleView changeSelectItemWithIndex:0];
    [self contentScrollView];
    [self.view addSubview:self.contentScrollView];
    [self.contentScrollView addSubview:self.canvasView];
    [self.canvasView addSubview:self.trendView];
    [self.trendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_canvasView.mas_left).offset(kScreenWidth*0);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    [self.canvasView addSubview:self.daysConditionsVie];
    [self.daysConditionsVie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*1);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    [self.canvasView addSubview:self.daysAlarmView];
    [self.daysAlarmView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_contentScrollView.mas_left).offset(kScreenWidth*2);
        make.bottom.top.mas_equalTo(_canvasView);
        make.width.mas_equalTo(kScreenWidth);
    }];
}

- (UIScrollView *)contentScrollView
{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.frame = CGRectMake(0, NavationBarHeight+50, kScreenWidth, kScreenHeight-TabBarHeight-NavationBarHeight-50);
        _contentScrollView.scrollEnabled = NO;
        _contentScrollView.contentSize = CGSizeMake(kScreenWidth * 3, 0);
        _contentScrollView.backgroundColor = [UIColor whiteColor];
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.bounces = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.delegate = self;
    }
    return _contentScrollView;
}



#pragma mark - 分栏滑动换页效果

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    if (_currentIndex == index) {
        return;
    }
    _currentIndex = index;
    _nowIndex = index;
    [self.titleView changeSelectItemWithIndex:index];
    [self loadSbuViewDataWithIndex:index];
}

- (void)SectionSelectIndex:(NSInteger)selectIndex {
    CGFloat offsetX = selectIndex * self.view.frame.size.width;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
    [self loadSbuViewDataWithIndex:selectIndex];
}



#pragma mark - 数据加载
- (void)loadSbuViewDataWithIndex:(NSInteger)index
{
    switch (index) {
        case 0:
            _trendView.param = _paramDic;
            [_trendView loadDataWithParam:_paramDic];
            break;
        case 1:
            _daysConditionsVie.param = _paramDic;
            [_daysConditionsVie loadDataWithParam:_paramDic];
            break;
        case 2:
            _daysAlarmView.param = _paramDic;
            [_daysAlarmView loadDataWithParam:_paramDic];
            break;
        default:
            break;
    }
}
- (void)firstLoadAllData
{
    
}
- (void)loadStatisticalDataWithDictionary:(NSDictionary *)dic
{
    _paramDic = dic;
    [self loadSbuViewDataWithIndex:_nowIndex];
}

#pragma mark - 点击事件


//右按钮点击事件
- (void)rightItemBtnClick:(UIButton *)sender
{
    sender.enabled = NO;
    MineViewController *mineVc = [MineViewController new];
    [mineVc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:mineVc animated:YES];
    sender.enabled = YES;
}

//发通知禁止tabBar跳转
- (void)allowProhibit
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"allowProhibit" object:nil];
}

//发通知允许tabBar跳转
- (void)jumpProhibit
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpProhibit" object:nil];
}

- (ManyDayView *)leftView
{
    if (!_leftView) {
        _leftView = [ManyDayView new];
        [self.navigationController.view addSubview:_leftView];
        [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view).offset(StatuBarHeight);
            make.left.right.bottom.mas_equalTo(self.view);
        }];
    }
    return _leftView;
}
//左按钮点击事件
- (void)leftItemBtnClick:(UIButton *)sender
{
    [super leftItemBtnClick:sender];
    Kweaself
    [weakSelf jumpProhibit];
    self.tabBarController.tabBar.hidden = YES;
    [self.leftView show];
    self.leftView.viewDisapper = ^{
        weakSelf.tabBarController.tabBar.hidden = NO;
    };
    self.leftView.searchDidClick = ^(NSDictionary *param) {
        
        [weakSelf setLeftBarButtonItemWithNormalImageName:@"screeSel" andSelectImageName:@"screeSel"];
        _paramDic = param;
        [weakSelf loadSbuViewDataWithIndex:weakSelf.nowIndex];
    };
}


@end
